﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SelfMadeMillionaire.Core.Specifications.Publications
{
	public class TrackingPortfolioPerfomanceSpecification : ISearchSpecification<Newsletter>
	{
		private int _count;
		private ProductType _productType;
		private SubscriptionType _minAccessLevel;

		public TrackingPortfolioPerfomanceSpecification(int showCount, ProductType productType, SubscriptionType minAccessLevel)
		{
			_count = showCount;
			_productType = productType;
			_minAccessLevel = minAccessLevel;
		}

		public IEnumerable<Newsletter> SatisfiedBy(IQueryable<Newsletter> entities)
		{
			return entities.Where(p => p.Status == (byte)StatusType.Active
									&& p.IssueType.Title == "Portfolio Performance"
									&& p.Letter == (byte)_productType
									&& p.MinAccessLevel <= (byte)_minAccessLevel)
							.OrderByDescending(x => x.Date)
                            .OrderByDescending(x => x.ID)
							.Take(_count);
		}
	}
}
